package com.tj.bo.account.server.services;

import com.fqgj.common.api.Page;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.DateUtil;
import com.google.common.collect.Lists;
import com.tj.bo.account.client.AccountQueryService;
import com.tj.bo.account.client.enums.GrantTypeEnum;
import com.tj.bo.account.client.enums.IncomeEnum;
import com.tj.bo.account.client.vo.AccountGrantsList;
import com.tj.bo.account.client.vo.AccountGrantsVO;
import com.tj.bo.account.client.vo.AccountVO;
import com.tj.bo.account.client.vo.BalanceVO;
import com.tj.bo.account.server.dao.AccountDao;
import com.tj.bo.account.server.dao.AccountFlowDao;
import com.tj.bo.account.server.entity.AccountEntity;
import com.tj.bo.account.server.entity.AccountFlowEntity;
import com.tj.bo.common.conf.enums.AmountTypeEnum;
import com.tj.bo.user.client.UserQueryService;
import com.tj.bo.user.client.vo.output.UserBriefInfoRO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 账号查询服务
 * User: anonymous
 * Date: 2020/06/11
 * Time: 下午2:16
 */
@Service
public class AccountQueryServiceImpl implements AccountQueryService {
    @Autowired
    private AccountDao accountDao;
    @Autowired
    private UserQueryService userQueryService;
    @Autowired
    private AccountFlowDao accountFlowDao;

    @Override
    public AccountVO getAccountInfo(String userCode) {
        AccountEntity accountEntity = accountDao.selectByUserCode(userCode);
        BigDecimal canWithdrawalAmount = calculateCanWithdrawalAmount(accountEntity);
        AccountVO accountVO = new AccountVO();
        BeanUtils.copyProperties(accountEntity, accountVO);
        accountVO.setCanWithdrawalAmount(canWithdrawalAmount);
        accountVO.setAllowWithdrawal(accountEntity.getAllowWithdrawal() == 1);
        return accountVO;
    }

    /**
     * 计算可提现金额
     * 目前固定规则： 可用余额-累计充值金额 的部分可提现
     * 改版之后的规则：可提现金额=availableBalance=总余额-赠送金额-冻结金额
     */
    private BigDecimal calculateCanWithdrawalAmount(AccountEntity accountEntity) {
        BigDecimal canWithdrawalAmount = accountEntity.getBalance().subtract(accountEntity.getGrants()).subtract(accountEntity.getFrozenAmount());
        return canWithdrawalAmount.compareTo(BigDecimal.ZERO) > 0 ? canWithdrawalAmount : BigDecimal.ZERO;
    }

    @Override
    public BalanceVO getBalanceInfo(String userCode) {
        BalanceVO balanceVO = new BalanceVO();
        UserBriefInfoRO userBriefInfoRO = userQueryService.getUserInfoByUserCode(userCode);
        if (userBriefInfoRO != null) {
            AccountEntity demoAccount = accountDao.selectByUserCode(userBriefInfoRO.getDemoUserCode());
            AccountEntity realAccount = accountDao.selectByUserCode(userBriefInfoRO.getRealUserCode());
            balanceVO.setDemoBalance(demoAccount != null ? demoAccount.getBalance().toString() : null);
            balanceVO.setRealBalance(realAccount != null ? realAccount.getBalance().toString() : null);
        }
        return balanceVO;
    }

    @Override
    public BalanceVO getRealUserBalanceInfo(String userCode) {
        AccountEntity realAccount = accountDao.selectByUserCode(userCode);
        BalanceVO balanceVO = new BalanceVO();
        balanceVO.setRealBalance(realAccount != null ? "PKR" + realAccount.getBalance().stripTrailingZeros().toPlainString() : "PKR0");
        return balanceVO;
    }

    @Override
    public AccountGrantsList getUserGrantsListByPage(String userCode, Integer pageNo) {
        Page page = new Page(20);
        page.setCurrentPage(pageNo);
        List<String> incomeList = Lists.newArrayList();
        incomeList.add(IncomeEnum.BONUS.name());
        incomeList.add(IncomeEnum.DEPOSIT.name());
        List<AccountFlowEntity> accountFlowEntities = accountFlowDao.getAccountFlowByTypeAndAmountType(userCode, "INCOME", incomeList, AmountTypeEnum.BONUS.getCode(), page);
        List<AccountGrantsVO> accountGrants = Lists.newArrayList();
        AccountGrantsList accountGrantsList = new AccountGrantsList();
        if(CollectionUtils.isNotEmpty(accountFlowEntities)){
            for(AccountFlowEntity entity : accountFlowEntities){
                AccountGrantsVO accountGrantsVO = new AccountGrantsVO();
                accountGrantsVO.setGetDate(DateUtil.getDate(entity.getGmtCreate(),"dd/MM/yyyy HH:mm:ss"));
                accountGrantsVO.setGrantType(GrantTypeEnum.getByIncome(entity.getSubType()).getName());
                accountGrantsVO.setGrants("PKR" + entity.getAmount().stripTrailingZeros().toPlainString());
                accountGrantsVO.setStatus("Recorded");
                accountGrants.add(accountGrantsVO);
            }
        }
        accountGrantsList.setAccountGrants(accountGrants);
        accountGrantsList.setHasNextPage(page.getCurrentPage() < page.getTotalPageNum());
        return accountGrantsList;
    }
}